

function Create (fn) {
    let ret = false

    return ({next, complete, error}) => {
        function nextFn (...args) {
            if (ret) {
                return
            }

            next(...args)
        }

        function completeFn (...args) {
            complete(...args)
            ret = true
        }

        function errorFn (...args) {
            error(...args)
        }

        fn({
            next: nextFn,
            complete: completeFn,
            error: errorFn
        })

        return () => ret = true
    }
}

let observerable = Create(observer => {
    setTimeout(() => {
        observer.next(1)
    }, 1000)

    observer.next(2)
    observer.complete(3)
})

const subject = {
    next: value => {
        console.log(value)
    },

    complete: console.log,
    error: console.error
}

let  unsubscribe = observerable(subject)


async function func () {
    return 2
}

func().then(console.log)

const getPosts = () => 
    new Promise((resolve, reject) => { 
        resolve([
            {name: 'a'},
            {name: 'b'},
            {name: 'c'},
            {name: 'd'}
        ])
    })

async function func2 () {
    try {
        const number = await func()
        const posts = await getPosts()
        console.log(number)
        console.log(posts)
    } catch (e) {
        console.log(e)
    }
}

func2()

const fs = require('fs')
// console.log(fs)
const readFilePromise = filename => new Promise((resolve, reject) => {
    console.log(filename)
    fs.readFile(filename, (err, data) => {
        console.log(1)
        if (err) {
            reject(err)
        } else {
            resolve(data)
        }
    })
})


async function manRead () {
    const text = await readFilePromise('../package.json')
    console.log(text.toString())
}

manRead()
